package file;

import example.FileUpload;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * 文件下载服务端处理器
 * 支绍昆 2018/10/15 0015
 */
public class FileDownLoadServerHandler extends ChannelInboundHandlerAdapter {

    private RandomAccessFile randomAccessFile;

    private int byteRead;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        System.out.println("channel active." + ctx.name());
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        System.out.println("channel read.");
        System.out.println("from client : " + msg);

        /*
        请求需要下载的文件与路径
        获取文件 并组装第一份内容返回
         */
        if (msg instanceof String){
            try {
                randomAccessFile = new RandomAccessFile(new File("D:\\桌面背景\\6dfe2d1ad80316e19704faa6b419a8e6.jpg"), "r");
                randomAccessFile.seek(0);
                byte[] byteArr = new byte[1024*10];
                if ((byteRead = randomAccessFile.read(byteArr)) != -1) {

                    FileUpload fileUpload = new FileUpload();
                    fileUpload.setEndPos(byteRead);
                    fileUpload.setStarPos(0);
                    fileUpload.setBytes(byteArr);
                    ctx.writeAndFlush(fileUpload);
                }else {
                    System.out.println("read file end.");
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        /*
        请求文件内容 参数流开始位置
        返回文件内容
         */
        else if (msg instanceof Integer){
            int start = (Integer) msg;
            if (start < 1){
                try {
                    randomAccessFile.close();
                } catch (IOException ignore) {}
                ctx.close();
                return;
            }
            try {
                int readLength = 1024 * 10;

                //判断文件结尾长度
                int left = (int) (randomAccessFile.length() - start);
                if (left < readLength){
                    readLength = left;
                }

                randomAccessFile.seek(start);
                byte[] byteArr = new byte[readLength];
                if ((byteRead = randomAccessFile.read(byteArr)) != -1) {

                    FileUpload fileUpload = new FileUpload();
                    fileUpload.setEndPos(readLength);
                    fileUpload.setStarPos(0);
                    fileUpload.setBytes(byteArr);
                    ctx.writeAndFlush(fileUpload);
                }else {
                    System.out.println("read file end.");
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        /*
        文件不存在
         */
        else {
            if (randomAccessFile != null){
                try {
                    randomAccessFile.close();
                } catch (IOException ignore) {}
            }
            ctx.close();
        }
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        System.out.println("channel inactive.");
        ctx.flush();
        ctx.close();

        if (randomAccessFile != null){
            randomAccessFile.close();
        }
    }

}
